<?php
/**
 * @name IndexController
 * @author xuebingwang
 * @desc 默认控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
*/
class IndexController extends MallController {

    public function init(){

        parent::init();
        //$this->layout->setLayoutFile('v1_layout.php');
    }

    public function spreadAction(){

        $user = session('user_auth');
        if(empty($user) || intval($user['subscribe']) < 1){
            $this->redirect('http://mp.weixin.qq.com/s?__biz=MzI3NjAxNzE3OA==&mid=208969297&idx=1&sn=9a49f2e1409e79d1168125f97d580f6a&scene=5#rd');
        }else{
            $this->redirect('index');
        }
    }

    public function cardAction(){

        $curl = new Curl();
        $products = array();

        $resp = $curl->setData(['category'=>31])->send('mall/product/getList');
        if(!empty($resp) && $resp['errcode'] == 0){
            $products = $resp['list'];
        }

        $this->getView()->assign('current_menu','card');
        $this->getView()->assign('products',$products);
    }

    /** 
    * 默认动作，首页
    * Yaf支持直接把Yaf\Request_Abstract::getParam()得到的同名参数作为Action的形参
    * 对于如下的例子, 当访问http://yourhost/y/index/index/index/name/yantze 的时候, 你就会发现不同
    */
    public function indexAction(){

        //读取缓存
        $cache_name = ROOT_PATH.'/runtime/cache/'.md5($this->getMCA()).'.php';
        if(file_exists($cache_name)){
            $this->getResponse()->setBody(file_get_contents($cache_name));
            return false;
        }

        $curl = new Curl();
        $resp = $curl->send('mall/public/getBannerList');
        
        $banners = array();
        if(!empty($resp) && $resp['errcode'] == 0){
            $banners = $resp['list'];
        }

        $modules = array();

        $resp = $curl->setData(['position'=>'floor'])->send('mall/product/getAMProducts');
//        var_dump($resp);
//        die;
        if(!empty($resp) && $resp['errcode'] == 0){
            $modules = $resp['list'];
        }

        $this->getView()->assign('banners',$banners);
        $this->getView()->assign('modules',$modules);

        //生成缓存文件
        file_put_contents($cache_name,$this->render($this->getAction()));
    }
}
